<template>
    <el-tree   :data="catalogs"  :props="catalogProps"

        :load="loadCatalogTreeNode"
        @node-click="onObjectNodeClick"
          lazy
        >

        </el-tree>
</template>

<script>
   
    const LABEL_DEPENDENCIES = "Dependencies";

    const LABEL_FLOWS = "Flows";

    const LABEL_TESTS = "Tests";

    const LABEL_MODELS = "Models";

    const LABEL_RESOURCES = "Resources";


    import Vue from 'vue';
    import {ConfigurationService} from '../apis/ConfigurationService.js';
    import 'element-ui/lib/theme-default/tree.css';
    import {Tree} from 'element-ui'
	Vue.component(Tree.name, Tree)

	
    export default {
        data () {
            return {
				catalogs :[
				   {
                      Id :"0",
                      Name :"Root"
				   }
				],

				catalogProps:{
                   label: 'Name',
                   children: 'children'
                }
            }
        },
		components:{},
        mounted() {
        }
		,methods:{

                
		     loadCatalogTreeNode(node, resolve){


                
                if(node.id=='0'){
                    ConfigurationService.FindProjects().then(
                         ret =>{
                              resolve(ret)
                         }
                    );
				}
				else
				if(node.level==1){//Project
					ConfigurationService.FindProjectVersionsByProject(node.data.Id).then(
                         ret =>{
							  ret.forEach(r=>{
							       r.Name =r.VersionLabel;	
						    	})
                              resolve(ret)
                         }
                    );
					
				}
				else
				if(node.level==2){//Version
				    let types =[LABEL_FLOWS,LABEL_TESTS,LABEL_MODELS,
					    LABEL_RESOURCES,
						LABEL_DEPENDENCIES]
						
					var nodes =[];
					
					types.forEach(type=> {
						
						nodes.push ({
						  Id : type+"_"+node.data.Id,
						  Name : type,
						  VersionId :node.data.Id
						
					   })
					})
					 	
					resolve(nodes)
					
				}
				else
				if(node.level==3 && node.data.Name==LABEL_FLOWS){//Flows
				    ConfigurationService.FindFlowsInProject(node.data.VersionId,0).then(
                         ret =>{
							  ret.forEach(r=>{
								   r.nodeType="FLOW"
							  })
                              resolve(ret)
                         }
                    );
					
				}
				else
				if(node.level==3 && node.data.Name==LABEL_TESTS){//Tests
				    ConfigurationService.FindFlowsInProject(node.data.VersionId,1).then(
                         ret =>{
							  ret.forEach(r=>{
								   r.nodeType="TEST"
							  })
                              resolve(ret)
                         }
                    );
					
				}
				else
				if(node.level==3 && node.data.Name==LABEL_MODELS){//Models
				    ConfigurationService.FindModelsInProject(node.data.VersionId).then(
                         ret =>{
							  ret.forEach(r=>{
								   r.nodeType="MODEL"
							  })
                              resolve(ret)
                         }
                    );
					
				}
				else
				if(node.level==3 && node.data.Name==LABEL_RESOURCES){//Resources
				    ConfigurationService.FindResourcesInProject(node.data.VersionId).then(
                         ret =>{
							  ret.forEach(r=>{
								   r.nodeType="RESOURCE"
							  })
                              resolve(ret)
                         }
                    );
					
				}
				else
				if(node.level==3 && node.data.Name==LABEL_DEPENDENCIES){//DEPENDENCIES
				    ConfigurationService.FindProjectDependencies(node.data.VersionId).then(
                         ret =>{
							   ret.forEach(r=>{
								   r.nodeType="DEPENDENCY" 
							       r.Name =r.TargetProjectVersionId.ProjectId.Name+"--"+ r.TargetProjectVersionId.VersionLabel;	
						    	})
                              resolve(ret)
                         }
                    );
					
				}
		     },
		     onObjectNodeClick(obj ,node ,self){
			 
			        this.$vnode.data.attrs.onObjectNodeClick(obj ,node ,self)
			 }


		}
    }
</script>

<style>

</style>